/*
 * Data Mashup Canonical Model. A canonical model for representing data mashups
 * Copyright (C) 2011 Carlos Rodriguez
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package eu.ict_omelette.mashup.canonicalmodel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author carlos
 */
public class CanonicalMashup implements Serializable {
    private String id;
    private String name;
    private String src;

    private List<Component> components;
    private List<GlobalParameter> globalParams;
    private List<MashupOutput> mashOutputs;
    private List<Dataflow> dataflows;

    public CanonicalMashup(){
        components = new LinkedList();
        globalParams = new LinkedList();
        mashOutputs = new LinkedList();
        dataflows = new LinkedList();
        
    }

    public static CanonicalMashup unserialize(String filename){
        CanonicalMashup cm = null;

        FileInputStream fis = null;
        ObjectInputStream in = null;
        try
        {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            cm = (CanonicalMashup)in.readObject();
            in.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return cm;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the src
     */
    public String getSrc() {
        return src;
    }

    /**
     * @param src the src to set
     */
    public void setSrc(String src) {
        this.src = src;
    }

    public void addComponent(Component comp) {
        components.add(comp);
    }

    public void removeComponent(Component comp){
        components.remove(comp);
    }

    public Component getComponent(String id){
        Iterator<Component> it = components.iterator();

        Component comp = null;
        while(it.hasNext()){
            Component aux = it.next();

            if(aux.getId().equals(id)){
                comp = aux;
                break;
            }
        }

        return comp;
    }

    public Iterator<Component> getComponents(){
        return components.iterator();
    }

    
    public void addGlobalParameter(GlobalParameter gParam){
        globalParams.add(gParam);
    }

    public void removeComponent(GlobalParameter gParam){
        globalParams.remove(gParam);
    }

    public GlobalParameter getGlobalParameter(String name){
        Iterator<GlobalParameter> it = globalParams.iterator();

        GlobalParameter gParam = null;
        while(it.hasNext()){
            GlobalParameter aux = it.next();
            
            if(aux.getName().equals(name)){
                gParam = aux;
                break;
            }
        }

        return gParam;
    }

    public Iterator<GlobalParameter> getGlobalParameters(){
        return globalParams.iterator();
    }

    public void addMashupOutput(MashupOutput mashOut){
        mashOutputs.add(mashOut);
    }

    public void removeMashupOutput(MashupOutput mashOut){
        mashOutputs.remove(mashOut);
    }

    public MashupOutput getMashupOutput(String name){
        Iterator<MashupOutput> it = mashOutputs.iterator();

        MashupOutput mashOut = null;
        while(it.hasNext()){
            MashupOutput aux = it.next();

            if(aux.getName().equals(name)){
                mashOut = aux;
                break;
            }
        }

        return mashOut;
    }

    public Iterator<MashupOutput> getMashupOutputs(){
        return mashOutputs.iterator();
    }

    public void addDataflow(Dataflow df){
        dataflows.add(df);
    }

    public void removeDataflow(Dataflow df){
        dataflows.remove(df);
    }

    public List<Dataflow> getDataflows(Component fromComponent, Component toComponent){
        Iterator <Dataflow> it = dataflows.iterator();

        List matchingDfs = new LinkedList();
        
        Dataflow df = null;
        while(it.hasNext()){
            Dataflow aux = it.next();

            if((aux.getFromComponent() == fromComponent) && (aux.getToComponent() == toComponent))
                matchingDfs.add(aux);
        }

        return matchingDfs;
    }

    public Iterator<Dataflow> getDataFlows(){
        return dataflows.iterator();
    }

    public void serialize(String filename){       
        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try
        {
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(this);
            out.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    @Override
    public String toString(){
        String outStr = "";

        outStr += "#############################################\n";
        outStr += "COMPONENTS:\n";
        Iterator<Component> compIt = components.iterator();
        while(compIt.hasNext()){
            Component comp = compIt.next();

            outStr += "\t" + comp.getName() + "[" +comp.getId()+ "]" + "\n";
            Iterator<InputParameter> ipIt = comp.getInputParameters();
            while(ipIt.hasNext()){
                InputParameter ip = ipIt.next();
                outStr += "\t\t" + ip.getName() + "=" + ip.getValue() + "\n";
            }
        }

        outStr += "DATAFLOWS:\n";
        Iterator<Dataflow> dfIt = dataflows.iterator();
        while(dfIt.hasNext()){
            Dataflow df = dfIt.next();
            Component fromComp = df.getFromComponent();
            Component toComp = df.getToComponent();

            String fromOp = df.getFromPort();
            String toIp = df.getToPort();

            outStr += "\tfrom " + fromComp.getType() + "(" + fromComp.getId() + ")" + "[" + fromOp + "]";
            outStr += "  to " + toComp.getType() + "(" + toComp.getId() + ")" + "[" + toIp + "]";
            outStr += "\n";
        }

        return outStr;       
    }
    
}